package com.foundryx.shared.exception;

import com.foundryx.shared.exception.i18n.MessageLookup;

/**
 * @className: BaseException
 * @author: ocxz1
 * @date: 2025/6/20 上午11:05
 * @Version: 1.0
 * @description: fx-share-resources
 */
public class BaseException extends RuntimeException {
    private static final int DEFAULT_ERROR_CODE = 500;

    private final int errorCode;
    private final String msgCode;
    private final Object[] args;

    public BaseException(Integer errorCode, String msgCode, String message, Object... args) {
        super(message);
        this.errorCode = errorCode == null ? DEFAULT_ERROR_CODE : errorCode;
        this.msgCode = msgCode;
        this.args = args;
    }

    public BaseException(Integer errorCode, String msgCode, String message, Throwable cause, Object... args) {
        super(message, cause);
        this.errorCode = errorCode == null ? DEFAULT_ERROR_CODE : errorCode;
        this.msgCode = msgCode;
        this.args = args;
    }

    protected static String resolveMessage(String msgCode, String defaultMsg, Object... args) {
        String message = MessageLookup.getMessage(msgCode, args);
        return message != null ? message : defaultMsg;
    }

    public String getMsgCode() {
        return msgCode;
    }

    public Object[] getArgs() {
        return args;
    }

    public int getErrorCode() {
        return errorCode;
    }
}
